﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Org.BouncyCastle.Crypto.Tls;
using System.Collections.Generic;
using System.Linq;
using Volo.Abp;
using Volo.Abp.EntityFrameworkCore.Modeling;

namespace EcoSystem.EntityFrameworkCore;

public static class EcoSystemDbContextModelCreatingExtensions
{
    public static void ConfigureEcoSystem(
        this ModelBuilder builder)
    {
        Check.NotNull(builder, nameof(builder));

        //配置相关实体
        builder.Entity<ConfigCategory>(b =>
        {
            b.ToTable(EcoSystemDbProperties.DbSystemTablePrefix + "config_category", EcoSystemDbProperties.DbSchema);
            b.Property(x => x.Id).HasMaxLength(128);
            b.HasMany(x => x.DefineFields).WithOne(x => x.CategoryInfo).HasForeignKey(x => x.Category).HasConstraintName("FKConfigCategoryId");
            b.Property<List<string>>("ModuleScope").HasObjectConversion().HasColumnType("VARCHAR(1024)");
            b.Property(x => x.ExtraProperties).HasColumnType("MEDIUMTEXT CHARACTER SET utf8mb4");
            b.ConfigureFullAuditedAggregateRoot();
            b.CreateComment();
        });

        builder.Entity<ConfigDefine>(b =>
        {
            b.ToTable(EcoSystemDbProperties.DbSystemTablePrefix + "config_define", EcoSystemDbProperties.DbSchema);
            b.Property(x => x.Category).IsRequired();
            b.Property(x => x.DefaultValue).HasColumnType("TEXT CHARACTER SET utf8mb4");
            b.Property(x => x.ControlProperty).HasColumnType("MEDIUMTEXT CHARACTER SET utf8mb4");
            b.Property(x => x.ExtraProperties).HasColumnType("MEDIUMTEXT CHARACTER SET utf8mb4");
            b.ConfigureFullAuditedAggregateRoot();
            b.CreateComment();
        });

        builder.Entity<ConfigSetting>(b =>
        {
            b.ToTable(EcoSystemDbProperties.DbSystemTablePrefix + "config_setting", EcoSystemDbProperties.DbSchema);
            b.Property(x => x.ExtraProperties).HasColumnType("MEDIUMTEXT CHARACTER SET utf8mb4");
            b.ConfigureFullAuditedAggregateRoot();
            b.CreateComment();
        });

        builder.Entity<TakeConfig>(b =>
        {
            b.ToTable(EcoSystemDbProperties.DbSystemTablePrefix + "take_config", EcoSystemDbProperties.DbSchema);
            b.Property<List<TakeRuleItem>>("RuleItems").HasObjectConversion().HasColumnName("RuleItems").HasColumnType("TEXT CHARACTER SET utf8mb4");
            b.Property(x => x.ExtraProperties).HasColumnType("MEDIUMTEXT CHARACTER SET utf8mb4");
            b.ConfigureFullAuditedAggregateRoot();
            b.CreateComment();
        });

        builder.Entity<TakeNumber>(b =>
        {
            b.ToTable(EcoSystemDbProperties.DbSystemTablePrefix + "take_number", EcoSystemDbProperties.DbSchema);
            b.TryConfigureCreationTime();
            b.TryConfigureLastModificationTime();
            b.CreateComment();
        });

        //数据字典相关实体
        builder.Entity<DictionaryItem>(b =>
        {
            b.ToTable(EcoSystemDbProperties.DbSystemTablePrefix + "dictionary_item", EcoSystemDbProperties.DbSchema);
            b.HasMany(x => x.Items).WithOne().HasForeignKey(x => x.ItemId);
            b.Property(x => x.ExtraProperties).HasColumnType("MEDIUMTEXT CHARACTER SET utf8mb4");
            b.ConfigureFullAuditedAggregateRoot();
            b.CreateComment();
        });

        builder.Entity<DictionaryValue>(b =>
        {
            b.ToTable(EcoSystemDbProperties.DbSystemTablePrefix + "dictionary_value", EcoSystemDbProperties.DbSchema);
            b.Property(x => x.ExtraProperties).HasColumnType("MEDIUMTEXT CHARACTER SET utf8mb4");
            b.ConfigureFullAuditedAggregateRoot();
            b.CreateComment();
        });


        //菜单权限
        builder.Entity<SystemMenu>(b =>
        {
            b.ToTable(EcoSystemDbProperties.DbSystemTablePrefix + "system_menu", EcoSystemDbProperties.DbSchema);
            b.Property(x => x.ExtraProperties).HasColumnType("MEDIUMTEXT CHARACTER SET utf8mb4");
            b.ConfigureFullAuditedAggregateRoot();
            b.CreateComment();
        });

        //数据权限业务配置
        builder.Entity<DataPermission>(b =>
        {
            b.ToTable(EcoSystemDbProperties.DbSystemTablePrefix + "data_permissions", EcoSystemDbProperties.DbSchema);
            b.Property(x => x.EnCode).IsRequired().HasMaxLength(64);
            b.Property(x => x.Name).HasMaxLength(64);
            b.Property(x => x.Description).HasMaxLength(256);
            b.Property<PermissionSourceOptions>("SourceOptions").HasColumnName("SourceOptions").HasObjectConversion().HasColumnType("MEDIUMTEXT CHARACTER SET utf8mb4");
            b.Property(x => x.ExtraProperties).HasColumnType("MEDIUMTEXT CHARACTER SET utf8mb4");
            b.ConfigureFullAuditedAggregateRoot();
            b.CreateComment();
        });

        //数据权限业务授权表
        builder.Entity<DataPermissionGrant>(b =>
        {
            b.ToTable(EcoSystemDbProperties.DbSystemTablePrefix + "data_permission_grants", EcoSystemDbProperties.DbSchema);
            b.Property(x => x.EnCode).IsRequired().HasMaxLength(128);
            b.Property(x => x.ProviderName).IsRequired().HasMaxLength(16);
            b.Property(x => x.ProviderKey).IsRequired().HasMaxLength(128);
            b.Property(x => x.AllowAuthorize).HasDefaultValue(true);
            b.ConfigureFullAuditedAggregateRoot();
            b.CreateComment();
        });

        //文件块
        builder.Entity<Bucket>(b =>
        {
            b.ToTable(EcoSystemDbProperties.DbTablePrefix + "bucket", EcoSystemDbProperties.DbSchema);
            b.Property(x => x.IsPublic).HasDefaultValue(false);
            b.Property(x => x.Status).HasDefaultValue(true);
            b.ConfigureFullAuditedAggregateRoot();
            b.CreateComment();
        });

        //资源目录
        builder.Entity<DirResource>(b =>
        {
            b.ToTable(EcoSystemDbProperties.DbTablePrefix + "dir_resource", EcoSystemDbProperties.DbSchema);
            b.ConfigureFullAuditedAggregateRoot();
            b.CreateComment();
        });

        //资源文件
        builder.Entity<FileResource>(b =>
        {
            b.ToTable(EcoSystemDbProperties.DbTablePrefix + "file_resource", EcoSystemDbProperties.DbSchema);
            b.Property<List<string>>("Tags").HasObjectConversion().HasColumnName("Tags").HasMaxLength(128);
            b.ConfigureFullAuditedAggregateRoot();
            b.CreateComment();
        });


        //区域信息
        builder.Entity<Region>(b =>
        {
            b.ToTable(EcoSystemDbProperties.DbSystemTablePrefix + "region", EcoSystemDbProperties.DbSchema);
            b.Property(x => x.Status).HasDefaultValue(true);
            b.ConfigureFullAuditedAggregateRoot();
            b.CreateComment();
        });

        //组织架构信息
        builder.Entity<Organization>(b =>
        {
            b.ToTable(EcoSystemDbProperties.DbSystemTablePrefix + "organization", EcoSystemDbProperties.DbSchema);
            b.Property(x => x.Status).HasDefaultValue(true);
            b.ConfigureFullAuditedAggregateRoot();
            b.CreateComment();
        });

        //企业信息
        builder.Entity<Enterprise>(b =>
        {
            b.ToTable(EcoSystemDbProperties.DbSystemTablePrefix + "enterprise", EcoSystemDbProperties.DbSchema);
            b.Property<AttachmentCollectionDto>("Attachments").HasObjectConversion().HasColumnName("Attachments").HasColumnType("TEXT CHARACTER SET utf8mb4");
            b.Property(x => x.Status).HasDefaultValue(true);
            b.ConfigureFullAuditedAggregateRoot();
            b.CreateComment();
        });

        //附件信息
        builder.Entity<Attachment>(b =>
        {
            b.ToTable(EcoSystemDbProperties.DbTablePrefix + "attachment", EcoSystemDbProperties.DbSchema);
            b.Property(x => x.ExtraProperties).HasColumnType("MEDIUMTEXT CHARACTER SET utf8mb4");
            b.ConfigureFullAuditedAggregateRoot();
            b.CreateComment();
        });

        //附件配置
        builder.Entity<AttachmentConfig>(b =>
        {
            b.ToTable(EcoSystemDbProperties.DbTablePrefix + "attachment_config", EcoSystemDbProperties.DbSchema);
            b.Property<AttachmentFieldCollection>("DefineFields").HasObjectConversion().HasColumnName("DefineFields").HasColumnType("MEDIUMTEXT CHARACTER SET utf8mb4");
            b.Property(x => x.ExtraProperties).HasColumnType("MEDIUMTEXT CHARACTER SET utf8mb4");
            b.ConfigureFullAuditedAggregateRoot();
            b.CreateComment();
        });

        //报表模块分类配置
        builder.Entity<ReportCategoryConfig>(b =>
        {
            b.ToTable(EcoSystemDbProperties.DbTablePrefix + "report_category_config", EcoSystemDbProperties.DbSchema);
            b.ConfigureFullAuditedAggregateRoot();
            b.CreateComment();
        });

        //报表配置
        builder.Entity<ReportConfig>(b =>
        {
            b.ToTable(EcoSystemDbProperties.DbTablePrefix + "report_config", EcoSystemDbProperties.DbSchema);
            b.Property(x => x.DataSource).HasColumnName("DataSource").HasObjectConversion().HasColumnType("MEDIUMTEXT CHARACTER SET utf8mb4");
            b.Property<ReportRequestOptions>("RequestOptions").HasColumnName("RequestOptions").HasObjectConversion().HasColumnType("MEDIUMTEXT CHARACTER SET utf8mb4");
            b.Property<List<ReportColumnConfig>>("ReportColumns").HasColumnName("ReportColumns").HasObjectConversion().HasColumnType("MEDIUMTEXT CHARACTER SET utf8mb4");
            b.Property<List<SummaryColumnConfig>>("SummaryColumns").HasColumnName("SummaryColumns").HasObjectConversion().HasColumnType("MEDIUMTEXT CHARACTER SET utf8mb4");
            b.Property<Dictionary<string, object>>("ReportProps").HasColumnName("ReportProps").HasObjectConversion().HasColumnType("MEDIUMTEXT CHARACTER SET utf8mb4");
            b.ConfigureFullAuditedAggregateRoot();
            b.CreateComment();
        });


        //HTTP API接口配置
        builder.Entity<HttpApiConfig>(b =>
        {
            b.ToTable(EcoSystemDbProperties.DbTablePrefix + "http_api_config", EcoSystemDbProperties.DbSchema);
            b.Property(x => x.RequestOptions).HasColumnName("RequestOptions").HasObjectConversion().HasColumnType("MEDIUMTEXT CHARACTER SET utf8mb4");
            b.Property(x => x.ScriptContent).HasColumnType("MEDIUMTEXT CHARACTER SET utf8mb4");
            b.Property(x => x.DemoSchema).HasColumnType("MEDIUMTEXT CHARACTER SET utf8mb4");
            b.Property(x => x.ErrorResponse).HasColumnType("TEXT CHARACTER SET utf8mb4");
            b.ConfigureFullAuditedAggregateRoot();
            b.CreateComment();
        });

        //页面通知消息接口
        builder.Entity<Notification>(b =>
        {
            b.ToTable(EcoSystemDbProperties.DbTablePrefix + "notification", EcoSystemDbProperties.DbSchema);
            b.ConfigureFullAuditedAggregateRoot();
            b.CreateComment();
        });
    }
}
